package com.itheima.controller;

import com.itheima.anno.LogOperate;
import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;


    @PostMapping("/upload")
    @LogOperate
    public Result upload(String username, Integer age, MultipartFile file) throws Exception {
        log.info("文件上传: {}, {}, {}", username, age, file);
        if (!file.isEmpty()){
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.indexOf("."));
            String uniqueFileName = UUID.randomUUID().toString().replace("-","" ) + extName;

            // 上传文件
            String url = aliyunOSSOperator.upload(file.getBytes(), uniqueFileName);
            return Result.success(url);

        }
        return Result.error("上传失败");
    }
}
